package com.example.twodb.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回参数的方法
 *
 * @date 2021/8/31 下午 12:28
 */
public class HttpResult extends HashMap<String, Object> {
    private HttpResult() {
        this(HttpStatus.SUCCESS);
    }

    private HttpResult(int code, String msg) {
        this.put("status", code);
        this.put("msg", msg);
    }

    private HttpResult(HttpStatus httpStatus) {
        this.put("status", httpStatus.getCode());
        this.put("msg", httpStatus.getMsg());
    }


    public HttpResult add(String name, Object value) {
        this.put(name, value);
        return this;
    }

    public HttpResult addAll(Object value) {
        if (value instanceof Map) {
            this.addAll(value);
        } else if (value instanceof List) {
            this.put("list", value);
        }
        return this;
    }

    public static HttpResult newSuccess() {
        return new HttpResult();
    }

    public static HttpResult newError() {
        return new HttpResult(HttpStatus.FAIL);
    }

    public static HttpResult newError(String msg) {
        return new HttpResult(HttpStatus.FAIL.getCode(), msg);
    }

    public static HttpResult newNoLogin() {
        return new HttpResult(HttpStatus.NO_LOGIN);
    }

    public static HttpResult newNoAuth() {
        return new HttpResult(HttpStatus.AUTH_ERROR);
    }

    public static HttpResult newNoFile() {
        return new HttpResult(HttpStatus.NO_FILE);
    }

}
